#!/usr/bin/python3.9
# -*- coding: UTF-8 -*-
# @Time : 2022/4/17 下午3:18
# @Author : chenchunchun

import os.path
import re
import profile


class TextAnalysisProgram(object):
    def __init__(self):
        self.split_article_content = []
        self.split_word_content = []
        self.word_num = 0
        self.sentence_num = 0
        pass

    def readFile(self, file_path):
        '''
        功能：读取文件
        :param file_path: String，文件路径
        :return:String，文件内容字符串
        '''
        if not os.path.isfile(file_path):
            raise TypeError(file_path + "不存在!")

        with open(file_path) as content:
            return content.read()

    def splitSentences(self, str):
        '''
        功能：分句
        :param str:文本字符串
        :return: 分句后的列表
        '''
        lst = re.split("(?<=[.!?])\s+", str)
        return lst

    def splitWords(self, str):
        '''
        功能：分词
        :param str:文本字符串
        :return: 分词后的列表
        '''
        lst = re.split(r"\b[\.,\s\n\r\n]+?\b", str)
        return lst

    def readFiles(self):
        document_file = input("Enter your document file: ")
        #document_file = "document.txt"
        self.split_article_content = self.splitSentences(self.readFile(document_file))
        query_file = input("Enter your query file: ")
        #query_file = "query.txt"
        self.split_word_content = self.splitWords(self.readFile(query_file))

    def findWords(self):
        '''
        功能：通过嵌套循环，依次从查询文本中依次选择单词，去文章中的一句话中进行比较。
        :return:
        '''
        for line in self.split_article_content:
            lst_line = self.splitWords(line) # 将每一句话中的单词转换为列表
            self.word_num += len(lst_line) # 统计每一句话中的单词数量并累加
            for word in self.split_word_content:
                if word in lst_line:
                    print(word + " is in " + line)
                else:
                    print(word + " is not in " + line)


    def count(self):
        self.sentence_num = len(self.split_article_content)
        print("该文档一共有" + str(self.word_num) + "个单词")
        print("该文档一共有" + str(self.sentence_num) + "句话")



def main():
    tap = TextAnalysisProgram()
    tap.readFiles()
    tap.findWords()
    tap.count()


#profile.run("main()")

if __name__ == '__main__':
    profile.run("main()")


